package com.apes.fn.server.productReceive;

import com.apes.fn.server.servicePlan.model.ServicePlanItem;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.scm.account.account.service.ProductAccountService;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.product.model.Product;
import com.apes.scm.masterdata.product.model.ProductUom;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Map;

/***
 * 配件领用单明细
 */

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = ProductReceiveItem.CONST_TABLE_NAME)
public class ProductReceiveItem extends BaseModel {

    public static final String CONST_TABLE_NAME = "stock_product_receive_item";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "领用单明细")
    private String id;

    @ManyToOne(optional = false)
    @JoinColumn(name = "product_receive_id", referencedColumnName = "id", nullable = false)
    @Comment("订单")
    private ProductReceive productReceive;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id", nullable = false)
    @Comment("公司")
    private PartyRole company;

//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "supplier_id", referencedColumnName = "id")
//    @Comment("供应商")
//    private PartyRole supplier;
//
//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "purchase_org_id", referencedColumnName = "id")
//    @Comment("采购组织编码")
//    private Organization purchaseOrg;  //采购组织

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_id", referencedColumnName = "id", nullable = false)
    @Comment("商品")
    private Product product;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_uom_id", referencedColumnName = "id", nullable = false)
    @Comment("计量单位")
    private ProductUom uom;

//    @NotNull
//    @Column(name = "tax_type", columnDefinition = "number(2) default 0")
//    @Comment("税别（0：增值税；1：营业税）")
//    private int taxType;

//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "tax_id", referencedColumnName = "id", nullable = false)
//    @Comment("税率")
//    private Tax tax;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "service_plan_item_id", referencedColumnName = "id")
    @Comment("服务方案明细")
    private ServicePlanItem servicePlanItem;



    public String queryCarLicense() {
        if( servicePlanItem == null ||
                servicePlanItem.getServicePlan().getFnCustomerExpand().getCarLicense() == null ) return null;
        return servicePlanItem.getServicePlan().getFnCustomerExpand().getCarLicense();
    }

    @Transient
    private Double qtySale = 0.00; // 销售数量

    public Double getQtySale() {
        if(servicePlanItem == null) return 0.00;
        return servicePlanItem.getSaleQty();
    }

    @Transient
    private Double qtyReceived = 0.00; // 已领用数量

    public Double getQtyReceived() {
        if(servicePlanItem == null) return 0.00;
        return servicePlanItem.getQtyReceive();
    }

    @Column(name = "qty_receive", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("领用数量")
    private double qtyReceive = 0;


    @Column(name = "qty_returned", nullable = false,  columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("已归还数量")
    private double qtyReturned = 0;

    public void setQtyReturned(double qtyReturned) {
        if( qtyReturned > this.qtyReceive ) throw new RuntimeException("归还数量不能大于领用数量");
        this.qtyReturned = qtyReturned;
    }

    @Column(name = "qty_to_Sales", nullable = false,  columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("转销售数")
    private double qtyToSales = 0;

    @Column(name = "remark", length = 120)
    @Comment("备注")
    private String remark;

    @Transient
    private Double marketableQty;  // 获取实时库存

    public Double getMarketableQty() {
        if (productReceive.getState().equals("approve")) return null;
        ProductAccountService productAccountService = SpringManager.getBean(ProductAccountService.class);
        Map map = productAccountService.getUomUsableQty(productReceive.getLocation(), product, uom, company);
        if (map.isEmpty()) return 0.00;
        return (Double) map.get("marketableQty");
    }

    @Transient
    private double qtyNotReturn = 0;
    public double getQtyNotReturn(){
        return qtyReceive - qtyReturned;
    }

}
